package com.wallpaper.weatherwallpaper;


import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.Toast;

import com.model.User;
import com.model.location.*;

public class SetCityActivity extends Activity {
	
//	private ArrayList<String> hotCityNames;
//	private GridView hotCityGridview;
	private GridView provinceGridview;
	private GridView cityGridView;
	

	private ArrayList<String> provinceNames;
	private ArrayList<String> cityNames;
	
	private HashMap<String,Province> provinces;
//	private ArrayList hotCities;
	
//	private ArrayAdapter<String> hotCityAryAdapter;
	private ArrayAdapter<String> provinceAryAdapter;
	private ArrayAdapter<String> cityAryAdapter;
	
	private User user;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_city);
        
        user = User.getUser(SetCityActivity.this);
        
        //初始化3个ArrayList
        provinceNames = new ArrayList<String>();
        cityNames = new ArrayList<String>();
    
        // 初始化省份列表
        provinces = new HashMap<String,Province>();
//        hotCities = new ArrayList();
    	Resources resource = getResources();
        initProvinces(resource);
        
//        hotCityNames=new ArrayList<String>();
//		hotCityGridview=(GridView)findViewById(R.id.hotcityGridview);	
//		hotCityAryAdapter=new ArrayAdapter<String>(SetCityActivity.this,R.layout.city_list_item,hotCityNames);		
//		hotCityGridview.setAdapter(hotCityAryAdapter);
//		
//		hotCityGridview.setOnItemClickListener(new GridView.OnItemClickListener(){
//
//			public void onItemClick(AdapterView<?> parent, View v, int position,
//					long arg3) {
//				//直辖市
//				if(position<6){
//					Province selectedProvince = (Province)hotCities.get(position);
//					user.setProvince(selectedProvince);
//				}
//				//城市
//				else{
//					City selectedCity = (City)hotCities.get(position);
//					user.setProvince(selectedCity.getBelongsTo());
//					user.setCity(selectedCity);
//				}
//				SetCityActivity.this.finish();
//			}
//			
//		});
		
		getProvinceNames();
		provinceGridview=(GridView)findViewById(R.id.provinceGridview);
		provinceAryAdapter=new ArrayAdapter<String>(SetCityActivity.this,R.layout.city_list_item,provinceNames);
		provinceGridview.setAdapter(provinceAryAdapter);
		
		provinceGridview.setOnItemClickListener(new GridView.OnItemClickListener(){

			public void onItemClick(AdapterView<?> parent, View v, int position,
					long arg3) {
				String provinceSelected=provinceNames.get(position);
				Province selectedProvince = provinces.get(provinceSelected);
				user.setProvince(selectedProvince);
				//如果是直辖市，直接关闭页面
				if(selectedProvince.isMunicipalities()){
					SetCityActivity.this.finish();
				}
				//如果是省份，那么就更新view
				else{
					cityNames = selectedProvince.getCityNames();
					//更新一下view
					cityAryAdapter = new ArrayAdapter<String>(SetCityActivity.this,R.layout.city_list_item,cityNames);
					cityAryAdapter.notifyDataSetChanged();
					cityGridView.setAdapter(cityAryAdapter);
				}
			}
			
		});
		
		cityGridView = (GridView) this.findViewById(R.id.cityGridview);
		cityAryAdapter = new ArrayAdapter<String>(SetCityActivity.this,R.layout.city_list_item,cityNames);
		cityGridView.setAdapter(cityAryAdapter);
		cityGridView.setOnItemClickListener(new GridView.OnItemClickListener(){
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String citySelected=cityNames.get(position);
				user.setCity(user.getProvince().findCity(citySelected));
				SetCityActivity.this.finish();
			}
		});
		
	}
    
    private void initProvinces(Resources resource){
    	 // 初始化xml的factory
        try {
			InputStream inStream = resource.openRawResource(R.raw.province);
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();	
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document document = builder.parse(inStream);
			Element root = document.getDocumentElement();
			NodeList provinceNodes = root.getElementsByTagName("province");
			for(int i=0;i<provinceNodes.getLength();i++){
				Element provinceNode = (Element) provinceNodes.item(i);
				Province province = new Province(
						Integer.parseInt(provinceNode.getAttribute("id")),
						provinceNode.getAttribute("pinyin"),
						provinceNode.getAttribute("name"),
						Boolean.parseBoolean(provinceNode.getAttribute("municipalities")));
				System.out.println(province.getProvinceName());
				provinces.put(province.getProvinceName(), province);
//				//如果是直辖市，放在热门城市列表中
//				if(province.isMunicipalities()){
//					hotCities.add(province);
//					hotCityNames.add(province.getProvinceName());
//				}
				//这个省份不是直辖市
				if(!province.isMunicipalities()){
					NodeList cityNodes = provinceNode.getElementsByTagName("city");
					for(int j=0;j<cityNodes.getLength();j++){
						Element cityNode = (Element) cityNodes.item(j);
						City city = new City(
								Integer.parseInt(cityNode.getAttribute("id")),
								cityNode.getAttribute("pinyin"),
								cityNode.getAttribute("name"),
								province);
						province.addCity(city);
//						//如果是热门城市的话，就放在热门城市列表里
//						if(cityNode.getAttribute("hotcity").equals("true")){
//							hotCities.add(city);
//							hotCityNames.add(city.getCityName());
//						}
					}
				}
			}
			inStream.close();
		} catch (Exception e) {e.printStackTrace();}
    }

    //TODO:Sgarlic 获得省份的String[]
	private void getProvinceNames() {
		Iterator<String> iterator = provinces.keySet().iterator();
		//ArrayList<String> tempList = new ArrayList<String>();
		provinceNames = new ArrayList<String>();
		while(iterator.hasNext())
			provinceNames.add(iterator.next());
	}
}
